package org.lq.ssm.Web;

import java.io.Serializable;

//通用与所有控制器往前端返回的数据
public class HttpResult<T> implements Serializable {
    private Integer code; //响应玛，1表示ok，0表示出错
    private String message;//响应消息 ， ok时这里有消息
    private String error;// 出错时的信息
    private T data;//数据

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static final Integer OK = 1;
    public static final Integer ERROR = 0;

    public HttpResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <DATA>HttpResult ok(DATA data, String message) {
        HttpResult<DATA> result = new HttpResult<>(OK,message,data);
        return result;
    }
    public static <DATA>HttpResult error(DATA data, String message) {
        HttpResult<DATA> result = new HttpResult<>(ERROR,message,data);
        return result;
    }

    @Override
    public String toString() {
        return "HttpResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", error='" + error + '\'' +
                ", data=" + data +
                '}';
    }
}
